﻿using AutoMapper;
using Infrastructure.Extend;
using Infrastructure.Web;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
    public class AdminUserApp
    {
        private IAdminUserRepository _service;
        public AdminUserApp(IAdminUserRepository Service)
        {
            _service = Service;
        }
        public PageDto<AdminUserDto> GetAdminUser(QueryListModel query, string userName, string nickName)
        {


            return _service.GetAdminUser(query,userName,nickName);

            //var expression = ExtLinq.True<AdminUser>();
            //if (!string.IsNullOrEmpty(userName))
            //{
            //    expression = expression.And(t => t.UserName.Contains(userName));
            //}
            //if (!string.IsNullOrEmpty(nickName))
            //{
            //    expression = expression.And(t => t.NickName.Contains(nickName));
            //}
            //var result = _service.IQueryable(expression).OrderByDescending(t => t.CreatedTime).Paging(query.Limit, query.Offset);
            //return new PageDto<AdminUserDto>()
            //{
            //    total = result.total,
            //    rows = Mapper.Map<List<AdminUser>, IList<AdminUserDto>>(result.rows.ToList())
            //};
        }
        public bool Save(AdminUser adminUser)
        {
            if (adminUser.Id == Guid.Empty)
            {
                adminUser.Id = Guid.NewGuid();
                if (_service.FindEntity(c => c.UserName == adminUser.UserName) != null)
                {
                    throw new KnownException("用户名已存在");
                }
                return _service.Insert(adminUser) > 0;
            }
            else
            {
                var entity = _service.FindEntity(adminUser.Id);
                if (_service.FindEntity(c => c.UserName == adminUser.UserName&&c.Id!=adminUser.Id) != null)
                {
                    throw new KnownException("用户名已存在");
                }
                entity.UserName = adminUser.UserName;
                entity.PassWord = adminUser.PassWord;
                entity.NickName = adminUser.NickName;
                entity.DepartId = adminUser.DepartId;
                return _service.Update(entity) > 0;
            }


        }

        public AdminUserDto GetAdminUser(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            return Mapper.Map<AdminUser, AdminUserDto>(entity);
        }
        public bool AdminUserDelete(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            return _service.Delete(entity) > 0;
        }
        public AdminUser Login(string userName, string password)
        {
            return _service.Login(userName, password);
        }
        public bool EditPassWord(Guid Id,string PassWord)
        {
            var entity = _service.FindEntity(Id);
            entity.PassWord = PassWord;
           return  _service.Update(entity)>0;
        }
        public bool SaveMenus(Guid Id, string Menus)
        {
            var entity = _service.FindEntity(Id);
            entity.Menus = Menus;
            return _service.Update(entity) > 0;

        }
        public bool UpdateLastLoginTime(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            entity.LastLoginTime = DateTime.Now;
            return _service.Update(entity) > 0;
        }
    }
}
